package com.googlecode.afdbaccess.web.common.interceptors;

import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;

import com.googlecode.afdbaccess.cache.resultconversion.exceptions.DataLayerWrappedResultSetConversionException;
import com.googlecode.afdbaccess.cache.resultconversion.exceptions.NoRowsReturnedException;
import com.googlecode.afdbaccess.web.common.exception.GuiLayerPageNotFoundException;

@DatabaseAccessing
@Interceptor
public class StandardDataLayerExceptionsParserInterceptor {
    @AroundInvoke
    public Object rollbackAtException(InvocationContext ctx) throws Exception {
        try {
            return ctx.proceed();
        } catch(DataLayerWrappedResultSetConversionException dwrsce) {
            if(dwrsce.getCause() instanceof NoRowsReturnedException) {
                throw new GuiLayerPageNotFoundException("Data does not exist", dwrsce);
            } else {
                throw dwrsce;
            }
        }
    }
}